<template>
  <div>
    <el-row>
      <el-col style="margin-right: 10px" :span="24" class="content-left">
        <el-row class="handleBox">
          <el-col :span="18">
            <template>
              <el-button type="normal" @click="edit('add')">新增</el-button>
              <el-button type="normal" @click="edit('edit')">编辑</el-button>
              <el-button type="normal" @click="toOpenAcia()"
                >关联结果表</el-button
              >
              <el-button type="normal" @click="del">删除</el-button>
            </template>
          </el-col>
          <el-col :span="6">
            <div class="handleIconBox">
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="export_porper">
                  <!-- 导出操作区域 -->
                  <el-button @click="exportExcel">导出</el-button>
                </div>
                <i
                  class="iconfont icon-export"
                  title="导出"
                  slot="reference"
                ></i>
              </el-popover>
              <!-- 导入 -->
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="import_porper">
                  <!-- 导入操作区域 -->
                  <el-upload
                    accept=".xls, .xlsx"
                    ref="upload"
                    :multiple="false"
                    :before-upload="beforeUpload"
                    :show-file-list="false"
                    action
                    :limit="1"
                    :http-request="uploadFile"
                  >
                    <el-button>导入</el-button>
                  </el-upload>
                </div>
                <i
                  class="iconfont icon-import"
                  title="导入"
                  slot="reference"
                ></i>
              </el-popover>
              <el-popover
                width="300"
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="search_porper">
                  <el-form
                    :model="form"
                    ref="form"
                    label-width="100px"
                    label-position="left"
                    size="mini"
                  >
                    <el-form-item label="生效日期：">
                      <el-date-picker
                        v-model="form.effectDt"
                        type="date"
                        style="width: 150px"
                        placeholder="选择日期"
                        :clearable="false"
                      ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="表中文名：" prop="tabCnName">
                      <el-input
                        style="width: 150px"
                        clearable
                        v-model.trim="form.tabCnName"
                        placeholder="请输入表中文名"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="表英文名：" prop="tabEnName">
                      <el-input
                        style="width: 150px"
                        clearable
                        v-model.trim="form.tabEnName"
                        placeholder="请输入表英文名"
                      ></el-input>
                    </el-form-item>

                    <el-form-item label="字段中文名：" prop="colDesc">
                      <el-input
                        style="width: 150px"
                        clearable
                        v-model.trim="form.colDesc"
                        placeholder="请输入字段中文名"
                      ></el-input>
                    </el-form-item>
                  </el-form>
                  <div style="text-align: center; padding: 10px">
                    <el-button
                      size="mini"
                      @click="(form.page = 1), searchFunc()"
                      >查询</el-button
                    >
                    <el-button size="mini" @click="searchClear">重置</el-button>
                  </div>
                </div>
                <i
                  class="iconfont icon-sousuo"
                  title="搜索"
                  slot="reference"
                ></i>
              </el-popover>
              <el-popover
                placement="bottom"
                width="150"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="operation_checkbox">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox
                      v-for="item in textData"
                      :label="item.label"
                      :key="item.prop"
                      >{{ item.label }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
                <i
                  class="iconfont icon-liebiaoshezhi"
                  title="列表设置"
                  slot="reference"
                ></i>
              </el-popover>
            </div>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-table ref="multipleTable" :data="tableData" @row-click="rowClick">
      <el-table-column
        width="55"
        type="selection"
        
      ></el-table-column>
      <el-table-column
        v-for="(item, key) in showData"
        align="center"
        :key="key"
        :prop="item.prop"
        :label="item.label"
        :show-overflow-tooltip="true"
        :render-header="item.render"
        :sortable="item.sortable"
        :formatter="item.formatter"
        :width="item.width"
        :min-width="item.minwidth"
      ></el-table-column>
    </el-table>
    <pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.page"
      @refresh="searchFunc"
      :page-size="form.size"
      :total="total"
    ></pagination>

    <!-- kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk -->
    <!-- kkkkkkkkkkkkkkkkkkkkkkkkk  Modal is here    kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk -->
    <!-- kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk -->
    <!-- 新增、编辑弹窗 -->
    <el-dialog
      :title="this.addOrEdit === 'add' ? '新增来源数据' : '编辑来源数据'"
      :visible.sync="displayEditForm"
    >
      <el-form
        :model="editForm"
        ref="editForm"
        :rules="editRules"
        size="small"
        :inline="true"
        label-position="right"
        label-width="auto"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="生效日期：" prop="effectDt">
              <el-date-picker
                @change="editForm.invalidDt = null"
                style="width: 170px"
                v-model="editForm.effectDt"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="失效日期：" prop="invalidDt">
              <el-date-picker
                :disabled="editForm.effectDt == null || editForm.effectDt == ''"
                style="width: 170px"
                v-model="editForm.invalidDt"
                type="date"
                placeholder="选择日期"
                :picker-options="limitPickerOptions"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属层级简称：" prop="belongLvlSname">
              <el-select
                style="width: 170px"
                @change="toEnaSub"
                v-model="editForm.belongLvlSname"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in formBelongLvlSnameOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属主题简称：" prop="belongSubjAreaSname">
              <el-select
                style="width: 170px"
                @change="toEnaTabName"
                :disabled="!enableBelongSubj"
                v-model="editForm.belongSubjAreaSname"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in formBelongSubjAreaSnameOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="表名：" prop="tabEnName">
              <el-select
                style="width: 170px"
                @change="toEnaColName"
                :disabled="!enableTabEnName"
                v-model="editForm.tabEnName"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in formTabEnNameOptions"
                  :key="item.tableName"
                  :label="item.comments"
                  :value="item.tableName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段名：" prop="colName">
              <el-select
                style="width: 170px"
                :disabled="!enableColName"
                v-model="editForm.colName"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in formColNameOptions"
                  :key="item.columnName"
                  :label="item.comments"
                  :value="item.columnName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字段分析属性代码：" prop="colAnalyAttrCd">
              <el-select
                style="width: 170px"
                v-model="editForm.colAnalyAttrCd"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in formColAnalyAttrCdOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editFormConfirm('editForm')">保存</el-button>
        <el-button @click="displayEditForm = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 关联结果表 -->
    <el-dialog title="关联结果表" :visible.sync="openAciateTab">
      <el-form
        :model="AciaForm"
        ref="AciaForm"
        :rules="AciaRules"
        size="small"
        :inline="true"
        label-position="right"
        label-width="auto"
      >
        <el-row>
          <el-form-item label="表中文名：" prop="tabCnName">
            <el-input
              clearable
              style="width: 100%"
              v-model="AciaForm.tabCnName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitAcia()">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <!-- 表格 -->
      <el-container style="padding-top: 10px">
        <el-row style="width: 100%">
          <el-table :data="AciaTableData">
            <el-table-column prop="tabCnName" label="表中文名" align="center">
            </el-table-column>
            <el-table-column prop="relaEnName" label="结果表" align="center">
            </el-table-column>
            <el-table-column
              prop="relaCnName"
              label="结果表中文名"
              align="center"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              min-width="25"
              align="center"
            >
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="aciaEdit(scope.row)"
                  >编辑</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-container>

      <!-- 分页栏 -->
      <el-container style="padding-top: 20px">
        <el-row style="width: 100%">
          <pagination
            @size-change="aciaPageSizeChange"
            @current-change="aciaCurPageChange"
            @refresh="aciaGeneralQuery"
            :current-page="AciaPage"
            :page-size="AciaPageSize"
            :total="AciaTotal"
          ></pagination>
        </el-row>
      </el-container>
    </el-dialog>

    <!-- 关联结果表-编辑 -->
    <el-dialog width="45%" title="关联表" :visible.sync="openAciaEdit">
      <el-form
        :model="AciaEditForm"
        ref="AciaEditForm"
        :rules="AciaEditRules"
        size="small"
        :inline="true"
        label-position="left"
        label-width="auto"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="星型模型表名：" prop="tabEnName">
              <el-input
                disabled
                style="width: 200px"
                v-model="AciaEditForm.tabEnName"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表中文名：" prop="tabCnName">
              <el-input
                disabled
                style="width: 200px"
                v-model="AciaEditForm.tabCnName"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结果表：" prop="relaEnName">
              <el-select
                style="width: 200px"
                v-model="AciaEditForm.relaEnName"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in AcitEditRelaEnNameOptions"
                  :key="index"
                  :label="item.comments"
                  :value="item.tableName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="submitAciaEdit()">保存</el-button>
        <el-button @click="openAciaEdit = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { DatasCenter } from "@/api/index";
import moment from "moment";
export default {
  data() {
    return {
      checkList: [],
      test01: "", //测试
      page: 1, //当前页
      pageSize: 20, //每页size
      total: 0, //总条数
      form: {
        effectDt: "", //生效日期 effectDt
        tabCnName: "", //表中文名字 tabCnName
        tabEnName: "", //表英文名字 tabEnName
        colDesc: "", //字段中文名 colDesc
        page: 1,
        size: 15,
      },
      qyEffectDt: "", //查询条件-生效日期 effectDt
      qyTabCnName: "", //查询条件-表中文名字 tabCnName
      qyTabEnName: "", //查询条件-表英文名字 tabEnName
      qyColDesc: "", //查询条件-字段中文名 colDesc
      pickerOptions: {
        //日期选择器
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      limitPickerOptions: {
        //日期选择器-带范围
        disabledDate: (time) => {
          return time.getTime() < this.editForm.effectDt;
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      tableData: [], //表格数据,
      displayEditForm: false, //显示编辑框弹窗
      expended: false, //标识展开
      excelFile: "", //选择导入的excel文件
      exportExcelData: null, // 导出时的excel数据流
      textData: [
        {
          prop: "effectDt",
          label: "生效日期",
          width: 125,
          sortable: true,
          align: "center",
          checkDisabled: false,
        },
        {
          prop: "invalidDt",
          label: "失效日期",
          width: 125,
          sortable: true,
          checkDisabled: false,
        },
        {
          prop: "belongLvlSname",
          label: "所属层级简称",
          checkDisabled: false,
          width: 125,
        },
        {
          prop: "belongSubjAreaSname",
          label: "所属主题简称",
          width: 125,
          checkDisabled: false,
          formatter: (row) => {
            if (
              row.belongSubjAreaSname == null ||
              row.belongSubjAreaSname.trim() == ""
            )
              return "";
            var some = "";
            some = this.formBelongSubjAreaSnameOptions.find(
              (v) => v.code == row.belongSubjAreaSname
            );
            if (some) {
              return some.name;
            } else {
              return "";
            }
          },
        },
        {
          prop: "tabEnName",
          label: "表英文名",
          width: 250,
          checkDisabled: false,
        },
        {
          prop: "tabCnName",
          label: "表中文名",
          width: 200,
          checkDisabled: false,
        },
        {
          prop: "colName",
          label: "字段英文名",
          width: 200,
          checkDisabled: false,
        },
        {
          prop: "colDesc",
          label: "字段中文名",
          width: 200,
          checkDisabled: false,
        },

        {
          prop: "colAnalyAttrCd",
          label: "字段分析属性",
          width: 150,
          checkDisabled: false,
          formatter: (row) => {
            if (row.colAnalyAttrCd == null || row.colAnalyAttrCd.trim() == "")
              return "";
            var some = "";
            some = this.formColAnalyAttrCdOptions.find(
              (v) => v.value == row.colAnalyAttrCd
            );
            if (some) {
              return some.label;
            } else {
              return "";
            }
          },
        },
        {
          prop: "modifyOperNo",
          label: "修订人",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            if (row.modifyOperNo) {
              return row.modifyOperNo;
            } else {
              return "-";
            }
          },
        },
        {
          prop: "modifyTime",
          label: "修订日期",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            if (row.modifyTime) {
              return moment(row.modifyTime).format("YYYY-MM-DD HH:mm:ss"); // 24小时制
            } else {
              return "-";
            }
          },
        },
      ],

      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMM 新增、编辑start MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      addOrEdit: "", //标识打开的是新增还是编辑
      selectRow: "", //编辑时，选中的行
      editForm: {
        effectDt: "", //新增弹窗-生效日期
        invalidDt: "", //新增弹窗-失效日期
        belongLvlSname: "", //新增弹窗-所属层级简称
        belongSubjAreaSname: "", //新增弹窗-所属主题简称
        tabEnName: "", //新增弹窗-表名
        colName: "", //新增弹窗-字段名
        colAnalyAttrCd: "2", //新增弹窗-字段分析属性代码
      },
      formBelongLvlSnameOptions: [], // 新增弹窗-所属层级下拉
      formBelongSubjAreaSnameOptions: [], // 新增弹窗-所属主题下拉
      formColAnalyAttrCdOptions: [], // 字段分析属性代码下拉项
      formTabEnNameOptions: [], // 表名下拉项
      formColNameOptions: [], // 字段名下拉项
      editRules: {
        //校验规则
        effectDt: [
          {
            required: true,
            message: "请选择生效日期",
            trigger: "change",
          },
        ],
        belongLvlSname: [
          {
            required: true,
            message: "请选择所属层级",
            trigger: "change",
          },
        ],
        belongSubjAreaSname: [
          {
            required: true,
            message: "请选择所属主题",
            trigger: "change",
          },
        ],
        tabEnName: [
          { required: true, message: "请选择表名", trigger: "change" },
        ],
        colName: [
          {
            required: true,
            message: "请选择字段名",
            trigger: "change",
          },
        ],
        colAnalyAttrCd: [
          {
            required: true,
            message: "请选择属性代码",
            trigger: "change",
          },
        ],
      },
      enableBelongSubj: false, //开启主题下拉
      enableTabEnName: false, //开启表名下拉
      enableColName: false, //开启字段名下拉
      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMM 新增、编辑end MMMMMMMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMM 关联结果表start MMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      openAciateTab: false, //是否打开关联结果表
      openAciaEdit: false, //是否打开编辑
      AciaForm: {
        tabCnName: "", //表中文名
      },
      AciaRules: {}, //验证规则
      AciaTableData: [], //表格数据
      AciaPage: 1, //当前页
      AciaPageSize: 10, //每页size
      AciaTotal: 1, //总条数

      AciaEditForm: {
        tabEnName: "", //关联结果表-编辑弹窗-星型模型表名
        tabCnName: "", //关联结果表-编辑弹窗-表中文名
        relaEnName: "", //关联结果表-编辑弹窗-结果表
      },
      AcitEditRelaEnNameOptions: [], //关联结果表-编辑弹窗-结果表-选项
      AciaEditRules: {
        //编辑窗验证规则
        tabEnName: [
          {
            required: true,
            message: "必填项不能为空",
            trigger: "change",
          },
        ],
        tabCnName: [
          {
            required: true,
            message: "必填项不能为空",
            trigger: "change",
          },
        ],
        relaEnName: [
          { required: true, message: "请选择一项", trigger: "change" },
        ],
      },
      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMM 关联结果表end  MMMMMMMMMMMM
      //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    };
  },
  computed: {
    // 展示字段
    showData() {
      let arr = this.textData.map((v) => {
        return v.label;
      });
      let valArr = [],
        showData = [];
      // 选中的字段排序
      arr.forEach((element) => {
        let flag = false;
        this.checkList.forEach((e) => {
          if (e == element) {
            flag = true;
          }
        });
        if (flag) {
          valArr.push(element);
        }
      });
      // 控制表列隐藏显示
      for (let i = 0; i < this.checkList.length; i++) {
        for (let j = 0; j < this.textData.length; j++) {
          if (this.textData[j].label == valArr[i]) {
            let item = { ...this.textData[j] };
            showData.push(item);
          }
        }
      }
      return showData;
    },
  },

  mounted() {
    //初始查询表格数据
    this.$nextTick(() => {
      this.getDict();
      this.searchFunc();
    });
  },
  created() {
    this.checkListInit();
  },
  watch: {},
  methods: {
    rowClick(row) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row, true);
    },
    checkListInit() {
      this.checkList = this.textData.map((v) => {
        return v.label;
      });
    },
    searchClear() {
      for (const key in this.form) {
        this.form[key] = "";
      }
      this.form.page = 1;
      this.form.size = 15;
    },
    getDict() {
      //字典数据
      // 查询所属层级简称
      DatasCenter.findHierarchyName({})
        .then((res) => {
          this.formBelongLvlSnameOptions = res.data;
        })
        .catch(function (error) {
          console.log(error);
        });
      // 查询所属主题简称
      DatasCenter.findThemeName({})
        .then((res) => {
          this.formBelongSubjAreaSnameOptions = res.data;
        })
        .catch(function (error) {
          console.log(error);
        });
      this.$getDict("base_dataType").then((res) => {
        if (res.code == 200) {
          this.formColAnalyAttrCdOptions = res.datas;
        }
      });
    },
    searchFunc() {
      //通用查询-主表格
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //校验通过
          let param = {
            page: this.form.page,
            size: this.form.size,
            effectDt: this.qyEffectDt,
            tabEnName: this.qyTabEnName,
            tabCnName: this.qyTabCnName,
            colDesc: this.qyColDesc,
          };
          DatasCenter.findPageSource(param)
            .then((res) => {
              this.handleQueryResult(res);
            })
            .catch(function (error) {
              console.log(error);
              this.$message({
                message: "查询服务器失败!",
                type: "warning",
                showClose: true,
                duration: this._GLOBAL_.duration,
              });
            });
        } else {
          return false;
        }
      });
    },
    handleQueryResult(data) {
      //处理findPage
      this.tableData = data.data;
      this.total = data.count;
    },
    resetForm(formName) {
      //重置表单
      this.$refs[formName].resetFields();
    },
    submitForm() {
      //提交表单
      this.page = 1;
      this.qyEffectDt = this.ObjDateFormatter(this.form.effectDt);
      this.qyTabCnName = this.form.tabCnName;
      this.qyTabEnName = this.form.tabEnName;
      this.qyColDesc = this.form.colDesc;
      this.searchFunc();
    },
    ObjDateFormatter(objDate) {
      //对象日期格式化
      if (objDate == null || objDate == "") {
        return "";
      }
      var date = new Date(objDate);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }
      return year + "-" + month + "-" + day;
    },
    stringDateFormatter(sDate) {
      //字符串日期格式化
      if (sDate == null || sDate == "") {
        return "";
      }
      var date = new Date(sDate);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }
      return year + "-" + month + "-" + day;
    },
    subjFormatter(val) {
      //所属主题-格式化
      if (val == null || val.trim() == "") return "";
      var some = "";
      some = this.formBelongSubjAreaSnameOptions.find((v) => v.code == val);
      if (some) {
        return some.name;
      } else {
        return "";
      }
    },
    colAnalyAttrCdFormatter(val) {
      //字段分析属性-格式化
      if (val == null || val.trim() == "") return "";
      var some = "";
      some = this.formColAnalyAttrCdOptions.find((v) => v.value == val);
      if (some) {
        return some.label;
      } else {
        return "";
      }
    },
    belongLvlSnameFormatter(val) {
      //所属层级-格式化
      if (val == null || val.trim() == "") return "";
      var some = "";
      some = this.formBelongLvlSnameOptions.find((v) => v.code == val);
      if (some) {
        return some.name;
      } else {
        return "";
      }
    },
    displayMessage(message, type) {
      //提示message
      this.$message({
        showClose: true,
        message: message,
        type: type,

        center: true,
      });
    },
    exportExcel() {
      //导出文件
      var effectDt = this.ObjDateFormatter(this.form.effectDt);
      if (effectDt == "") {
        effectDt = this.ObjDateFormatter(new Date());
      }

      let param = {
        effectDt: effectDt, //生效日期,导出此生效日期之前的数据
        tabEnName: this.form.tabEnName, //表英文名
        tabCnName: this.form.tabCnName, //表中文名
        colDesc: this.form.colDesc, //字段中文名
        page: 1,
        size: 999999,
      };
      let self = this;
      DatasCenter.expSource(param)
        .then((res) => {
          if (res) {
            var filename = decodeURI(res.headers["content-disposition"]).split(
              "="
            )[1];
            let contenttype =
              filename.substring(filename.lastIndexOf("."), filename.length) ===
              "xlsx"
                ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
                : "application/vnd.ms-excel;charset=utf-8";
            const content = res.data;
            const blob = new Blob([content], {
              type: contenttype,
            });
            let url = window.URL.createObjectURL(blob);
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            // let name = '来源数据表定义.xlsx'
            link.setAttribute("download", filename);
            document.body.appendChild(link);
            link.click();
          }
        })
        .catch(function (error) {
          console.log(error);
          self.$message({
            type: "error",
            message: "导出失败!",
          });
        });
    },
    beforeUpload(file) {
      //上传文件前得检查
      var name = file.name;
      var type = name.substring(name.indexOf("."), name.length);
      if (type === ".xls" || type === ".xlsx") {
        return true;
      }
      this.$message({
        message: "只能选择xls或者xlsx格式的文件上传",
        type: "warning",
        showClose: true,
      });
      return false;
    },
    uploadFile(item) {
      //导入文件
      this.excelFile = item.file;
      let form = new FormData();
      // 文件对象
      form.append("uploadfile", this.excelFile);
      var that = this;
      DatasCenter.impSource(form)
        .then((res) => {
          if (res.code == 200) {
            that.$message({
              type: "success",
              message: "文件：" + that.excelFile.name + "上传成功",
            });
            that.$refs.upload.clearFiles();
            that.searchFunc();
          } else {
            that.$message({
              type: "error",
              message: res.message,
            });
            that.$refs.upload.clearFiles();
          }
        })
        .catch(function () {
          that.$message({
            type: "warning",
            message: "文件内容与模板不相符，导入失败"
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.form.size = val;
      this.searchFunc();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.form.page = val;
      this.searchFunc();
    },
    handlePaginationCurrent(value) {
      //当前页改变
      this.page = Number(value);
      this.searchFunc();
      window.removeEventListener("keydown", function () {});
    },
    handlePaginationRefresh(boolean) {
      //刷新按钮
      boolean && this.searchFunc();
    },
    handlePaginationSizes(value) {
      //每页多少条改变
      this.pageSize = Number(value);
      this.page = 1;
      this.searchFunc();
    },

    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMM 新增、编辑弹窗start  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    edit(val) {
      //新增弹窗
      //   this.addOrEdit = "edit";
      //   if (row == undefined) {
      //     this.addOrEdit = "add";
      //   }
      this.addOrEdit = val;
      if (this.addOrEdit === "add") {
        this.editForm.effectDt = ""; //新增弹窗-生效日期
        this.editForm.invalidDt = ""; //新增弹窗-失效日期
        this.editForm.belongLvlSname = ""; //新增弹窗-所属层级简称
        this.editForm.belongSubjAreaSname = ""; //新增弹窗-所属主题简称
        this.editForm.tabEnName = ""; //新增弹窗-表名
        this.editForm.colName = ""; //新增弹窗-字段名
        this.editForm.colAnalyAttrCd = "2"; //新增弹窗-字段分析属性代码
        (this.enableBelongSubj = false), //开启主题下拉
          (this.enableTabEnName = false), //开启表名下拉
          (this.enableColName = false), //开启字段名下拉
          (this.displayEditForm = true);
      }
      if (this.addOrEdit === "edit") {
        let selection = this.$refs.multipleTable.selection;
        if (selection.length != 1) {
          return this.$message.warning("请选择一条数据进行修改");
        }
        this.selectRow = selection[0];
        let row = selection[0];
        this.editForm.effectDt = new Date(row.effectDt);
        this.editForm.invalidDt =
          row.invalidDt == null ? null : new Date(row.invalidDt);
        this.editForm.colAnalyAttrCd = row.colAnalyAttrCd;
        this.editForm.belongLvlSname = row.belongLvlSname;
        this.editForm.belongSubjAreaSname = row.belongSubjAreaSname;
        let param = {
          // tableName: `dms_inf_${
          tableName: `t_${
            this.editForm.belongLvlSname == null
              ? ""
              : this.editForm.belongLvlSname
          }${this.editForm.belongSubjAreaSname}`,
        };
        DatasCenter.findTableNames(param)
          .then((res) => {
            this.formTabEnNameOptions = res.data;
          })
          .catch(function (error) {
            console.log(error);
          });
        this.editForm.tabEnName = row.tabEnName;

        let param2 = {
          tabEnName: `${this.editForm.tabEnName}`,
        };
        DatasCenter.getColumns(param2)
          .then((res) => {
            this.formColNameOptions = res.data;
          })
          .catch(function (error) {
            console.log(error);
          });
        this.editForm.colName = row.colName;
        this.enableBelongSubj = true;
        this.enableTabEnName = true;
        this.enableColName = true;
        this.displayEditForm = true;
      }
      this.$nextTick(() => {
        this.$refs["editForm"].clearValidate();
      });
    },
    toEnaSub() {
      //选择了层级
      this.enableBelongSubj = true;
    },
    toEnaTabName() {
      //选择了主题
      this.editForm.tabEnName = "";
      this.editForm.colName = "";
      let param = {
        // tableName: `dms_inf_${
        tableName: `t_${
          this.editForm.belongLvlSname == null
            ? ""
            : this.editForm.belongLvlSname
        }${this.editForm.belongSubjAreaSname}`,
      };
      DatasCenter.findTableNames(param)
        .then((res) => {
          this.formTabEnNameOptions = res.data;
          this.formColNameOptions = [];
        })
        .catch(function (error) {
          console.log(error);
        });
      this.enableTabEnName = true;
    },
    toEnaColName() {
      //选择了表名
      this.editForm.colName = "";
      let param2 = {
        tabEnName: `${this.editForm.tabEnName}`,
      };
      DatasCenter.getColumns(param2)
        .then((res) => {
          this.formColNameOptions = res.data;
        })
        .catch(function (error) {
          console.log(error);
        });
      this.enableColName = true;
    },
    editFormConfirm(val) {
      //新增、编辑保存
      var formName = val;
      if (formName == undefined) {
        formName = "form";
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //校验通过
          var tabCnName = this.editForm.tabEnName
            ? this.formTabEnNameOptions.find(
                (e) => e.tableName === this.editForm.tabEnName
              ).comments
            : "";
          var colDesc = this.editForm.colName
            ? this.formColNameOptions.find(
                (e) => e.columnName === this.editForm.colName
              ).comments
            : "";
          this.editForm.effectDt = this.ObjDateFormatter(
            this.editForm.effectDt
          );
          this.editForm.invalidDt = this.ObjDateFormatter(
            this.editForm.invalidDt
          );
          var uuid = this.addOrEdit === "add" ? "" : this.selectRow.uuid;

          let param2 = {
            uuid: uuid,
            effectDt: this.editForm.effectDt,
            invalidDt: this.editForm.invalidDt,
            belongLvlSname: this.editForm.belongLvlSname,
            belongSubjAreaSname: this.editForm.belongSubjAreaSname,
            tabEnName: this.editForm.tabEnName,
            tabCnName: tabCnName,
            colName: this.editForm.colName,
            colDesc: colDesc,
            colAnalyAttrCd: this.editForm.colAnalyAttrCd,
          };
          DatasCenter.checkUniqueDefSource(param2)
            .then((res) => {
              this.handleFormCheckUnique(res, tabCnName, colDesc);
            })
            .catch(function (error) {
              console.log(error);
            });
        } else {
          //校验失败
          return false;
        }
      });
    },
    handleFormCheckUnique(response, tabCnName, colDesc) {
      if (response.data == true) {
        var uuid = this.addOrEdit === "add" ? "" : this.selectRow.uuid;
        let param2 = {
          uuid: uuid,
          effectDt: this.editForm.effectDt,
          invalidDt: this.editForm.invalidDt,
          belongLvlSname: this.editForm.belongLvlSname,
          belongSubjAreaSname: this.editForm.belongSubjAreaSname,
          tabEnName: this.editForm.tabEnName,
          tabCnName: tabCnName,
          colName: this.editForm.colName,
          colDesc: colDesc,
          colAnalyAttrCd: this.editForm.colAnalyAttrCd,
        };
        DatasCenter.saveDefSource(param2)
          .then((response) => {
            this.handleFormSave(response);
          })
          .catch(function (error) {
            console.log(error);
          });
      } else {
        this.displayMessage(response.message, "error");
      }
    },
    handleFormSave(response) {
      if (response.code == 200) {
        //服务端保存成功
        this.displayEditForm = false;
        this.$message({
          type: "success",
          message: "保存成功!",
        });
        this.searchFunc();
        return;
      }
      this.displayMessage(response.message, "error");
    },
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMM 新增、编辑弹窗end  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMM 关联结果表start  MMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    toOpenAcia() {
      //点击关联结果表
      this.AciaPage = 1;
      this.AciaPageSize = 15;
      this.AciaTotal = 0;
      this.AciaTableData = [];
      this.AciaForm.tabCnName = "";

      let param2 = {
        page: this.AciaPage,
        size: this.AciaPageSize,
        tabCnName: "",
      };
      DatasCenter.findPageBaseDataRela(param2)
        .then((res) => {
          this.handleAciaFindPage(res);
        })
        .catch(function (error) {
          console.log(error);
        });
      this.openAciateTab = true;
    },
    submitAcia() {
      //提交表单
      var formName = "AciaForm";
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //校验通过
          this.AciaPage = 1;

          let param2 = {
            page: this.AciaPage,
            size: this.AciaPageSize,
            tabCnName: this.AciaForm.tabCnName,
          };
          DatasCenter.findPageBaseDataRela(param2)
            .then((res) => {
              this.handleAciaFindPage(res);
            })
            .catch(function (error) {
              console.log(error);
            });
        } else {
          //校验失败
          return false;
        }
      });
    },
    aciaGeneralQuery() {
      //通用查询
      var formName = "AciaForm";
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //校验通过
          let param2 = {
            page: this.AciaPage,
            size: this.AciaPageSize,
            tabCnName: this.AciaForm.tabCnName,
          };
          DatasCenter.findPageBaseDataRela(param2)
            .then((res) => {
              this.handleAciaFindPage(res);
            })
            .catch(function (error) {
              console.log(error);
            });
        } else {
          //校验失败
          return false;
        }
      });
    },
    handleAciaFindPage(response) {
      //处理findPage
      this.AciaTotal = response.total;
      this.AciaTableData = response.data;
    },
    aciaEdit(row) {
      //编辑按钮
      this.AciaEditForm.tabEnName = row.tabEnName;
      this.AciaEditForm.tabCnName = row.tabCnName;
      this.AciaEditForm.relaEnName = row.relaEnName;
      this.AcitEditRelaEnNameOptions = [];

      let param = {
        tableName: "",
      };
      DatasCenter.findTableNames(param)
        .then((res) => {
          this.AcitEditRelaEnNameOptions = res.data;
        })
        .catch(function (error) {
          console.log(error);
        });
      this.openAciaEdit = true;
    },
    submitAciaEdit() {
      //编辑保存
      console.log(
        this.AcitEditRelaEnNameOptions,
        "this.AcitEditRelaEnNameOptions"
      );
      console.log(this.AciaEditForm.relaEnName, "this.AciaEditForm.relaEnName");
      var relaCnName = this.AcitEditRelaEnNameOptions.find(
        (e) => e.tableName === this.AciaEditForm.relaEnName
      ).comments;
      let param2 = {
        tabEnName: this.AciaEditForm.tabEnName,
        tabCnName: this.AciaEditForm.tabCnName,
        relaEnName: this.AciaEditForm.relaEnName,
        relaCnName: relaCnName,
      };
      DatasCenter.saveBaseDataRela(param2)
        .then((res) => {
          this.hdAciaEditSave(res);
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    hdAciaEditSave(response) {
      //处理关联结果表-编辑保存
      if (response.code == 200) {
        this.$message.success("保存成功");
        this.openAciaEdit = false;
        this.aciaGeneralQuery();
      } else {
        this.displayMessage(response.message, "error");
      }
    },

    aciaPageSizeChange(val) {
      //每页大小改变
      this.AciaPage = 1;
      this.AciaPageSize = val;
      this.aciaGeneralQuery();
    },
    aciaCurPageChange(val) {
      //当前页改变
      this.AciaPage = val;
      this.aciaGeneralQuery();
    },
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMM 关联结果表end  MMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMM 删除弹窗start  MMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    del() {
      let selection = this.$refs.multipleTable.selection;
      if (selection.length < 1) {
        return this.$message.warning("请选择要删除的数据");
      }
      //删除按钮
      this.$confirm("删除这条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // var param = { id: selection[0].uuid }
          let param = {
            ids: selection.map((v) => v.uuid),
          };
          DatasCenter.removeDefSource(param)
            .then((res) => {
              this.handleRemove(res);
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch(() => {
          // console.log(e)
        });
    },
    handleRemove(response) {
      //处理删除结果
      if (response.code == 200) {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        this.searchFunc();
      } else {
        this.$message({
          type: "warning",
          message: response.message,
        });
        this.searchFunc();
      }
    },
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMM 删除弹窗end  MMMMMMMMMMMM
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  },
};
</script>
